package com.cindata

import com.cindata.dictionary.*
import org.springframework.security.access.annotation.Secured

import java.text.DecimalFormat

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Secured(['isAuthenticated()'])
@Transactional
class RiskWarningLevelRuleController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) 
    {
        params.max = Math.min(max ?: 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        max = params.max
        def name = params['name']
        def offset = params.offset
        def sql = "  from RiskWarningLevelRule m where 1=1 "
        if (name)
        {
            sql += " and name like '%${name}%' "
        }
        def sql1 = " select count(*) " + sql
        sql += "order by m.priority asc "
        def riskWarningLevelRuleList = RiskWarningLevelRule.executeQuery(sql, [max: max, offset: offset]).asList()
        def count = RiskWarningLevelRule.executeQuery(sql1)
        respond riskWarningLevelRuleList, model: [riskWarningLevelRuleList: riskWarningLevelRuleList, riskWarningLevelRuleCount: count[0]]
    }

    def show(RiskWarningLevelRule riskWarningLevelRule) {
        respond riskWarningLevelRule
    }

    def create() {
        params['targetUri'] = request.getHeader("referer")
        respond new RiskWarningLevelRule(params)
    }

    @Transactional
    def save(RiskWarningLevelRule riskWarningLevelRule) {

        println riskWarningLevelRule?.errors

        if (riskWarningLevelRule == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningLevelRule.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningLevelRule.errors, view:'create'
            return
        }

        riskWarningLevelRule.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'riskWarningLevelRule.label', default: 'RiskWarningLevelRule'), riskWarningLevelRule.id])
                redirect url: params['targetUri']
            }
            '*' { respond riskWarningLevelRule, [status: CREATED] }
        }
    }

    def edit(RiskWarningLevelRule riskWarningLevelRule) 
    {
        def targetUri = request.getHeader("referer")
        params['targetUri'] = targetUri
        respond riskWarningLevelRule
    }

    @Transactional
    def update(RiskWarningLevelRule riskWarningLevelRule) {
        if (riskWarningLevelRule == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (riskWarningLevelRule.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond riskWarningLevelRule.errors, view:'edit'
            return
        }

        riskWarningLevelRule.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'riskWarningLevelRule.label', default: 'RiskWarningLevelRule'), riskWarningLevelRule.id])
                redirect url: params['targetUri']
            }
            '*'{ respond riskWarningLevelRule, [status: OK] }
        }
    }

    @Transactional
    def delete(RiskWarningLevelRule riskWarningLevelRule) {

        if (riskWarningLevelRule == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        def targetUri = request.getHeader("referer")
        
        riskWarningLevelRule.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'riskWarningLevelRule.label', default: 'RiskWarningLevelRule'), riskWarningLevelRule.id])
                redirect url: targetUri
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'riskWarningLevelRule.label', default: 'RiskWarningLevelRule'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
